var $importFailedDataList = $('#importFailedDataList');

//防止表头与表格不对齐
$(window).resize(function () {
    $importFailedDataList.bootstrapTable('resetView');
});

$(function () {
    // 初始化下拉菜单
    $('#operateType').selectpicker({
        // liveSearch: false,
        sanitize: true,
        size: 10,
        width: 180
    });
    searchImportFailedDataList();

    if ($("#qx").val() ==1){
        if ("false" == $("#isSuper").val()){
            $("#companySelectDiv").hide();
        }else {
            loadAllCompanyList();
        }
    }
});

/**
 * 导入失败日志--分页列表页
 */
function searchImportFailedDataList(){
    $importFailedDataList.bootstrapTable('destroy');
    $importFailedDataList.bootstrapTable({
        classes: 'table table-hover table-bordered table-striped ',
        url: '/customerProductOrderImport/orderFailedDataList',            // 请求地址
        method: 'post',                                         // 请求方式
        contentType: "application/x-www-form-urlencoded",       // 请求内容类型
        dataType: "json",                                       // 数据类型
        striped: true,                                          // 是否显示行间隔色
        sortable: true,                                         // 是否启用排序
        sortOrder: "desc",                                      // 排序方式
        sortName: 'operateTime',                                   // 排序字段
        cache: false,                                           // 是否使用缓存
        showColumns: false,                                     // 显示隐藏列
        showRefresh: false,                                     // 显示刷新按钮
        showToggle: false,                                      // 切换显示样式
        showFooter: false,                                      // 显示Table脚部
        pagination: true,                                       // 是否显示分页
        showPaginationSwitch: false,                            // 是否显示分页按钮
        clickToSelect: true,                                    // 是否启用点击选中行
        singleSelect: false,
        minimumCountColumns: 2,                                 // 最少要显示的列数
        undefinedText: '-',                                      // cell没有值时显示
        sidePagination: "server",                               // 分页方式：client客户端分页，server服务端分页
        uniqueId: 'importFailureLogId',
        idField: 'importFailureLogId',
        pageSize: 10,                                            // 每页的记录行数
        pageNumber: 1,                                          // 初始化加载第1页，默认第1页
        pageList: "[10, 20, 50]",                            // 可供选择的每页的行数
        queryParams: function (params) {
            var temp = {
                pageSize : params.limit,
                pageIndex : (params.offset / params.limit) + 1,
                sortName : params.sort, //排序列名
                sortOrder : params.order,//排序方式
                qx : $("#qx").val(), //区分页面权限
                orderNumber : $("#importName").val(),//导入名称
                //导入什么业务：1-线索；2-客户；3-订单；4-合同；5-用户；6-跟进记录; 7-联系人
                operateType : $("#dateType").val(),
                companyId : $("#companySelect").val()
            }
            return temp;
        },//查询参数
        columns: [
            {title: '主键ID', field: 'importFailureLogId', visible: false},
            {title: '导入名称', field: 'importName', align: 'left', valign: 'middle'},
            {title: '失败原因', field: 'reasonForFailure', align: 'left', valign: 'middle'},
            {title: '导入类型', field: 'operateType', align: 'left', valign: 'middle', formatter: operateTypeFormat},
            {title: '操作人', field: 'operatePeopleName', align: 'left', valign: 'middle'},
            {title: '操作时间', field: 'operateTime', align: 'left', valign: 'middle'}
        ],
        responseHandler: function (result) {
            if (result.code == "000000") {
                var obj = {
                    "rows": result.data,
                    "total": result.totalRecords//总条数
                };
            } else {
                var obj = {
                    "total": 0,
                    "rows": [],
                }
            }
            return obj;
        },
        onLoadSuccess: function (data) {
            //加载成功时执行
            //$("[data-toggle='tooltip']").tooltip();//加这个是为了让操作列的提示图标显示在上方
        },
        onLoadError: function (status) {
            if (status == 403) {
                $.alert("权限不足！禁止访问！");
            } else {
                //加载失败时执行
                $.alert("加载失败！");
            }
        },
        formatLoadingMessage: function () {
            return "请稍等，加载中...";
        },
        formatNoMatches: function () {
            return '无符合条件的记录';
        },
    });
}

/**
 * 加载全部公司信息
 */
function loadAllCompanyList() {

        $.getJSON("/company/getCompanyList", function (data) {
            if (data.code == '000000') {
                var optionHtml = '';
                var optionArr = data.data;
                var optionSelected = $('#companySelect option:selected').val();
                //公司搜索条件下拉框
                var searchCompanyOptionHtml = '<option value="">请选择所属分公司</option>';
                optionArr.map(function (item, index) {
                    searchCompanyOptionHtml += '<option value="' + item.companyId + '">' + item.companyName + '</option>';
                });
                $('#companySelect').html(searchCompanyOptionHtml);
                var selectDate = [];
                for (var i = 0, len = data.data.length; i < len; i++) {
                    var company = data.data[i];
                    var selectCompany = {};
                    selectCompany.id = company.companyId;
                    selectCompany.name = company.companyName;
                    selectDate.push(selectCompany);
                }
                /*abs.render({
                    elem: "#search",
                    data: selectDate,
                    init_value: [1],
                    body_height: 300,
                    checkbox: true,
                    name: "test"
                });*/
                $('#searchoption-list').css('z-index',1000);
                $('#companySelect').selectpicker({
                    sanitize: true,
                    size: 8
                });
            }
        });



}

/**
 * --列表序列号
 */
function indexFormat(value, row, index) {
    return index + 1;
}

function selectImportType(type) {
    if (type == 'all') {//全部
        $('.all').addClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("");
    }
    if (type == 'account') {//账号
        $('.account').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("5");
    }
    if (type == 'customer') {//客户
        $('.customer').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("2");
    }
    if (type == 'contact') {//联系人
        $('.contact').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("7");
    }
    if (type == 'follow') {//跟进记录
        $('.follow').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("6");
    }
    if (type == 'contract') {//合同
        $('.contract').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("4");
    }
    if (type == 'order') {//订单
        $('.order').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("3");
    }
    if (type == 'money') {//到款记录
        $('.money').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("8");
    }
    if (type == 'vip') {//会员卡
        $('.vip').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.clue').removeClass('active-second');
        $("#dateType").val("9");
    }
    if (type == 'clue') {//线索
        $('.clue').addClass('active-second');
        $('.all').removeClass('active-second');
        $('.account').removeClass('active-second');
        $('.customer').removeClass('active-second');
        $('.contact').removeClass('active-second');
        $('.follow').removeClass('active-second');
        $('.contract').removeClass('active-second');
        $('.order').removeClass('active-second');
        $('.money').removeClass('active-second');
        $('.vip').removeClass('active-second');
        $("#dateType").val("1");
    }
    $importFailedDataList.bootstrapTable('refreshOptions', {pageNumber: 1});
}

//导入什么业务：1-线索；2-客户；3-订单；4-合同；5-用户；6-跟进记录; 7-联系人
function operateTypeFormat(value, row, index) {
    if (value == null || value == undefined) {
        return "-";
    } else if (1 == value) {
        return "<span class='label label-success-light'>线索</span>";
    } else if (2 == value) {
        return "<span class='label label-info-light'>客户</span>";
    } else if (3 == value){
        return "<span class='label label-warning-light'>订单</span>";
    } else if (4 == value){
        return "<span class='label label-purple-light'>合同</span>";
    } else if (5 == value){
        return "<span class='label label-cyan-light'>用户</span>";
    } else if (6 == value){
        return "<span class='label label-brown-light'>跟进记录</span>";
    } else if (7 == value){
        return "<span class='label label-yellow-light'>联系人</span>";
    } else if (8 == value){
        return "<span class='label label-yellow-light'>回款</span>";
    } else if (9 == value){
        return "<span class='label label-yellow-light'>会员卡</span>";
    }
}


